#include "CanCharger.h"
#include <stdio.h>
#include <stdlib.h>
#include "drv_can.h"
#include "systemdata.h"

#define HK_ADYPOWER_CHARGER			//铁城、安德普充电器
#define HK_ADYPOWER_CHARGER_ID		0x18FF50E5		//CAN_ID
#define HK_ADYPOWER_CHARGER_BMS_ID	0x1806E5F4		//对应的BMS_ID

typedef union{
	struct{
		uint8_t HARD_ERROR		:1;//硬件故障
		uint8_t	CHARGER_TEMP	:1;//充电机温度
		uint8_t	INPUT_VOL		:1;//输入电压
		uint8_t START_STATE		:1;//启动状态
		uint8_t TRANS_STATE		:1;//通信状态
		uint8_t RM_BIT			:3;//预留
	}bits;
	uint8_t status;
}HK_AdyPower_Status;

static HK_AdyPower_Status 	hkAdyPowerStatus = {.status = 0};//铁城、安德普充电机状态

static void parseHkOrAdyPowerProtocol(CanTransData_t *canMsg){//铁城、安德普充电器
	CanTransData_t sendFrame;
	SysRunData_t *runinfo = getSysRunData();
	uint16_t tempVol = 0;
	uint16_t tempCur = 0;
	tempVol = canMsg->data[0];
	tempVol <<= 8;
	tempVol |= canMsg->data[1];
	
	tempCur = canMsg->data[2];
	tempCur <<= 8;
	tempCur |= canMsg->data[3];
	
	hkAdyPowerStatus.status = canMsg->data[4];
//	printf("chargerOutVol = %d.%dV\n",tempVol/10,tempVol%10);
//	printf("chargerOutCur = %d.%dA\n",tempCur/10,tempCur%10);
	
	if(hkAdyPowerStatus.status == 0){
		runinfo->comLinkState.Bits.can_obc_link = 1;
	}else{
		//runinfo->comLinkState.Bits.can_obc_link = false;
		if(hkAdyPowerStatus.bits.HARD_ERROR){
			printf("hkAdyPower charger hardware fault!\n");
		}
		if(hkAdyPowerStatus.bits.CHARGER_TEMP){
			printf("hkAdyPower charger temperature high protect!\n");
		}
		if(hkAdyPowerStatus.bits.INPUT_VOL){
			printf("hkAdyPower charger input voltage error!\n");
		}
		if(hkAdyPowerStatus.bits.START_STATE){
			printf("hkAdyPower charger in stop state!\n");
		}
		if(hkAdyPowerStatus.bits.TRANS_STATE){
			printf("hkAdyPower charger transport timeout!\n");
		}
	}
	sendFrame.ExtId = HK_ADYPOWER_CHARGER_BMS_ID;
	sendFrame.dataLen = 8;
	sendFrame.data[0] = (runinfo->chgReqVol>>8)&0xff;
	sendFrame.data[1] = runinfo->chgReqVol&0xff;
	sendFrame.data[2] = (runinfo->chgReqCur>>8)&0xff;
	sendFrame.data[3] = runinfo->chgReqCur&0xff;
	
	if(runinfo->curProtByte.bit.shortCurProt|runinfo->curProtByte.bit.chgCUr1Prot|runinfo->curProtByte.bit.chgCur2Prot|
		runinfo->curProtByte.bit.chgCur3Prot|(bool)runinfo->volProtByte.cellHgVolProt|
		runinfo->tempProtByte.bit.chgTempHgProt|runinfo->tempProtByte.bit.mosTempHgProt){
		sendFrame.data[4] = 1;//电池保护，充电机关闭输出
	}else if(runinfo->tempProtByte.bit.chgTempLowProt){//低温保护
		sendFrame.data[4] = 2;//充电低温保护，加热模式
	}else{
		sendFrame.data[4] = 0;//正常充喽
	}
	sendFrame.data[5] = runinfo->batSoc&0xff;
	sendFrame.dataLen = 8;
	can_send_msg(&sendFrame);
}

static void parseCanMsg(CanTransData_t *canMsg){
	//printf("canMsg->ExtId = 0x%08x\n",canMsg->ExtId);
	SysRunData_t *runinfo = getSysRunData();
	switch(canMsg->ExtId){
		case HK_ADYPOWER_CHARGER_ID:{
			runinfo->comLinkState.Bits.can_obc_timeout = 0;
#ifdef HK_ADYPOWER_CHARGER
			parseHkOrAdyPowerProtocol(canMsg);//安德普充电器
#endif
		}break;
	}
}

void can_charger_process(CanTransData_t *canData){
	SysRunData_t *runinfo = getSysRunData();
	parseCanMsg(canData);
}

